<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;


class c_split_redis_list_2_x extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature											= 'c_split_redis_list_2_x {--redis-list-key-name=} {--split-number=5} {--confirm-tips=n}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description											= 'developer : BREEZEER split master redis to X sub list '."\n".
																	  '  version   : 1.0.0';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
		# hi gay coder fucking,waiting for ...
		#
		#
		(new c_split_redis_list_2_x_implements($this))->on_handle();

        return 0;
    }
}

class c_split_redis_list_2_x_implements
{
	public function __construct($p_this)
	{
		$this->m_p_this												= $p_this			;
		$this->m_cHelper											= app()->gcHelper	;
	}
	public function	on_handle()
	{
		# get command parameter
		#
		#
		$this->m_redis_list_key_name								= strtolower($this->m_p_this->option('redis-list-key-name'))		;	# master redis list key name
		$this->m_split_number										= intval(strtolower($this->m_p_this->option('split-number')))		;	# split number
		$this->m_confirm_tips										= strtolower($this->m_p_this->option('confirm-tips'))				;   # confirm tips

		#
		#
		#
		if($this->m_redis_list_key_name=='')
		{
			$this->m_cHelper->echo_to_console_with_ln('redis list key name is not empty')	;return;
		}

		#
		#
		#
		if($this->m_split_number==''||$this->m_split_number<=1)
		{
			$this->m_cHelper->echo_to_console_with_ln('split number must over 1')			;return;
		}

		#
		#
		#
		$can_runing = false;if(in_array($this->m_confirm_tips,['no','n']))
		{
			$can_runing	= true;
		}

		#
		#
		#
		if($can_runing===false)
		{
			$this->m_cHelper->console()->read	(
													"are your sure to want split master redis list $this->m_redis_list_key_name to $this->m_split_number sub list [y/n] "
													,
													function() use(&$can_runing)
													{
														$can_runing = true;
													}
												);
		}

		#
		#
		#
		if($can_runing===true)
		{
			for(;;)
			{
				sleep (1);
				$this->split_redis_list($this->m_redis_list_key_name,$this->m_split_number);
			}
		}

	}
	private function split_redis_list($key_name,$split_number)
	{
		# get current master redis list total length
		#
		#
		if (0 == $this->m_cHelper->gfGetRedis()->list($key_name)->length()/* redis master list is empty */)
		{
			return;
		}

		# split redis to x
		#
		#
		$this->m_cHelper->gfGetRedis()->list($key_name)->set_enable_debug(true)->split_2_x	
		(
			$split_number
			,
			function ($ar_key_list)
			{
				$ar	= [];foreach($ar_key_list as $one_list_key/* one sub redis list key name */)
				{
					$o						= new \stdclass();
					$o->redis_key_name		= $one_list_key;
					$o->redis_key_length	= $this->m_cHelper->gfGetRedis()->list($one_list_key)->length();
					$ar []					= $o;
				}
				$this->m_cHelper->echo_to_console_with_ln ("[".$this->m_cHelper->gfGetCurrentLocalDate()."] ".$this->m_cHelper->gfJsonEncode ($ar));
			}
		);

	}
}